package cn.schoolwow.workflow.module.task.action.flow.option;

import cn.schoolwow.quickdao.domain.transaction.Transaction;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.DeployNodeType;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDefinitionOption;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowNode;
import cn.schoolwow.workflow.entity.WorkFlowTask;
import cn.schoolwow.workflow.module.common.domain.WorkFlowTaskType;
import cn.schoolwow.workflow.module.common.flow.AddWorkFlowHistoryFlow;
import cn.schoolwow.workflow.module.task.action.domain.CompleteTaskOption;
import cn.schoolwow.workflow.module.task.action.flow.common.AutomaticPassNodeCommonFlow;
import cn.schoolwow.workflow.module.task.action.flow.common.SetNodeAssignerFinishedOptionCommonFlow;

import java.util.List;

public class HandleIntelligentOptionFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        shouldContinue(flowContext);
        completeTask(flowContext);
        checkShouldAutomaticPassNode(flowContext);
    }

    @Override
    public String name() {
        return "处理自动去重选项";
    }

    private void shouldContinue(FlowContext flowContext){
        WorkFlowDefinitionOption workFlowDefinitionOption = flowContext.getInstanceData(WorkFlowDefinitionOption.class);
        if(null==workFlowDefinitionOption){
            flowContext.brokenCurrentFlow("");
            return;
        }
        if(!workFlowDefinitionOption.intelligent){
            flowContext.brokenCurrentFlow("");
        }
        WorkFlowNode nextNode = flowContext.checkData("nextNode", WorkFlowNode.class);
        //判断下一个节点为审批节点时才自动通过
        DeployNodeType deployNodeType = DeployNodeType.getDeployNodeType(nextNode.getType());
        if(!DeployNodeType.AuditNode.equals(deployNodeType)){
            flowContext.brokenCurrentFlow("");
        }
    }

    private void completeTask(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        Transaction transaction = flowContext.checkInstanceData(Transaction.class);

        transaction.commit();

        List<String> assignerList = transaction.query(WorkFlowTask.class)
                .addQuery("instanceId", workFlowInstance.getId())
                .addQuery("complete", true)
                .addQuery("type", WorkFlowTaskType.AuditTask.value)
                .addColumn("assigner")
                .execute()
                .getSingleColumnList(String.class);
        if(assignerList.isEmpty()){
            flowContext.brokenCurrentFlow("当前实例无任何人完成任务");
        }
        long update = transaction.query(WorkFlowTask.class)
                .addQuery("instanceId", workFlowInstance.getId())
                .addQuery("type", WorkFlowTaskType.AuditTask.value)
                .addInQuery("assigner", assignerList)
                .addUpdate("complete", true)
                .execute()
                .update();
        if(update==0){
            flowContext.brokenCurrentFlow("智能去重当前未更新任何审批任务");
        }
    }

    private void checkShouldAutomaticPassNode(FlowContext flowContext){
        WorkFlowNode nextNode = flowContext.checkData("nextNode", WorkFlowNode.class);
        CompleteTaskOption completeTaskOption = flowContext.checkInstanceData(CompleteTaskOption.class);

        flowContext.startFlow(new SetNodeAssignerFinishedOptionCommonFlow())
                .putCurrentCompositeFlowInstanceData(nextNode)
                .execute();

        if(completeTaskOption.setNextNode){
            flowContext.startFlow(new AddWorkFlowHistoryFlow())
                    .putCurrentCompositeFlowData("message", "根据[自动去重]选项自动通过当前任务")
                    .execute();

            //自动通过当前任务
            flowContext.startFlow(new AutomaticPassNodeCommonFlow())
                    .putCurrentCompositeFlowData("passNode", nextNode)
                    .execute();
        }
    }
}
